{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "U.S. state capitals overlayed on a map of the U.S\n",
    "-------------------------------------------------\n",
    "This is a layered geographic visualization that shows US capitals\n",
    "overlayed on a map.\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# category: geographic\n",
    "\n",
    "import altair as alt\n",
    "alt.data_transformers.enable('json')\n",
    "from vega_datasets import data\n",
    "\n",
    "states = alt.topo_feature(data.us_10m.url, 'states')\n",
    "capitals = data.us_state_capitals.url\n",
    "\n",
    "# US states background\n",
    "background = alt.Chart(states).mark_geoshape(\n",
    "    fill='lightgray',\n",
    "    stroke='white'\n",
    ").properties(\n",
    "    title='US State Capitols',\n",
    "    width=700,\n",
    "    height=400\n",
    ").project('albersUsa')\n",
    "\n",
    "# Points and text\n",
    "hover = alt.selection(type='single', on='mouseover', nearest=True,\n",
    "                      fields=['lat', 'lon'])\n",
    "\n",
    "base = alt.Chart(capitals).encode(\n",
    "    longitude='lon:Q',\n",
    "    latitude='lat:Q'\n",
    ")\n",
    "\n",
    "text = base.mark_text(dy=-5, align='right').encode(\n",
    "    alt.Text('city', type='nominal'),\n",
    "    opacity=alt.condition(~hover, alt.value(0), alt.value(1))\n",
    ")\n",
    "\n",
    "points = base.mark_point().encode(\n",
    "    color=alt.value('black'),\n",
    "    size=alt.condition(~hover, alt.value(30), alt.value(100))\n",
    ").properties(\n",
    "    selection=hover\n",
    ")\n",
    "\n",
    "background + points + text\n"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "env": {},
   "interrupt_mode": "signal",
   "language": "python",
   "metadata": {},
   "name": "python3"
  },
  "language": "python"
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
